<template>
  <div>
    <el-dialog :visible.sync='isShow' title='岗位权限分配' width="800px" @close="closeBrand" @open="openDevInfo"
      :close-on-click-modal=false>
      <div v-loading="loading">
        <el-card>
          <el-form :model="postRole" ref="postRole" size="mini" :rules="rules" label-position="left" label-width="125px">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label='应用编号 :' prop='appNum'>
                  <el-select disabled v-model="postRole.appNum" placeholder="请选择应用">
                    <el-option v-for="item in appList" :key="item.value" :label="item.AppNum+'-'+item.AppName" :value="item.AppNum"
                      :disabled="item.disabled">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="岗位编号:" prop="postNum">
                  <el-input disabled v-model="postRole.postNum" name="postNum"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="岗位名称:" prop="postName">
                  <el-input disabled v-model="postRole.postName" name="postName"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="法人号:" prop="legalPersonNum">
                  <el-input disabled v-model="postRole.legalPersonNum" name="legalPersonNum"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="法人名称:" prop="legalPersonName">
                  <el-input disabled v-model="postRole.legalPersonName" name="legalPersonName"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
        <el-card style="margin-top:10px;">
          <el-row :gutter="20">
            <el-col :span="23">
              <el-tree :data="roleList" :props='{children: "children",label: "rolename"}'
                show-checkbox ref="tree" node-key="rolenum" :default-checked-keys='postRole.selectItem'
                highlight-current>
              </el-tree>
            </el-col>
          </el-row>
        </el-card>
        <el-row style="margin:20px 0;text-align:center">
          <el-button size="mini" @click="isShow=false">取消</el-button>
          <el-button type="primary" size="mini" @click="brandAdd">确认</el-button>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import request from "@/common/utils/request";
  import postRole from "@/message/deviceInfoManage/brand/brand-create";
  import appQuery from "@/message/shared/appquery";
  import postRoleQry from "@/message/userManage/post/post-role-query";
  import postRoleSub from "@/message/userManage/post/post-role-submit";



  export default {
    props: {
      option: Object
    },
    data() {
      return {
        loading: false,
        appList: [], //应用下拉表数据
        roleList:[],
        postRole: {
          appNum: "", //应用编号
          postNum: "", //岗位编号
          postName: "", //岗位名称
          legalPersonNum: "", //法人号
          legalPersonName: "", //法人名称
          selectItem: []
        },
        rules: {}
      };
    },
    computed: {
      isShow: {
        get: function () {
          return this.option.isShow;
        },
        set: function (newValue) {
          this.option.isShow = newValue;
        }
      }
    },
    methods: {
      openDevInfo() {
        this.loading = true;
        this.dataInit();

      },
      dataInit() {
        this.postRole.postNum = this.option.currentRow.postnum;
        this.postRole.postName = this.option.currentRow.postname;
        this.postRole.appNum = this.option.currentRow.appnum;
        this.postRole.legalPersonNum = this.$store.getters.LegalPersonNum;
        this.postRole.legalPersonName = this.$store.getters.LegalPersonName;
        let resBody = new appQuery();
        resBody.data.UserNum = this.$store.getters.UserNum;
        request(resBody)
          .then(response => {
            this.appList = response.RspInfo.Result || [];
          })
          .catch(error => {
            this.loading = false;
            console.error("查询错误", error);
          });
        let resBody1 = new postRoleQry();
        resBody1.data.AppNum =  this.postRole.appNum;
        resBody1.data.PostNum = this.postRole.postNum;
        resBody1.data.LegalPersonNum = this.postRole.legalPersonNum;
        request(resBody1)
          .then(response => {
            if (response.SYS_HEAD.ReturnCode === "000000") {
              this.roleList = response.RspInfo.pageList ||[];
              this.roleList.forEach(item =>{
                item.rolename = item.rolenum+"-"+item.rolename;
              })
              let checkList = response.RspInfo.pageList1 ||[];
              let check= [];
              checkList.forEach(element => {
                check.push(element.entynum);
              });
              this.postRole.selectItem =check
            }
            this.loading = false;
          })
          .catch(error => {
            this.loading = false;
            console.error("查询错误", error);
          });

      },
      closeBrand() {
        this.appList= []; //应用下拉表数据
        this.roleList=[];
        this.postRole= {
          appNum: "", //应用编号
          postNum: "", //岗位编号
          postName: "", //岗位名称
          legalPersonNum: "", //法人号
          legalPersonName: "", //法人名称
          selectItem: []
        },
        this.$refs["postRole"].clearValidate() //清楚表单验证
      },
      brandAdd() {
            this.loading = true;
            let resBody = new postRoleSub();
            resBody.data.AppNum = this.postRole.appNum;
            resBody.data.PostNum = this.postRole.postNum;
            resBody.data.LegalPersonNum = this.postRole.legalPersonNum;
            let check =this.$refs.tree.getCheckedKeys();
            let selectList =[];
            check.forEach(item => {
              selectList.push({"PrivLegeId":item});
            })
            resBody.data.GroupTranList = selectList;
            request(resBody)
              .then(response => {
                if (response.SYS_HEAD.ReturnCode === "000000") {
                  this.loading = false;
                  this.$message({
                    type: "success",
                    message: "保存成功!"
                  });
                  this.isShow = false;
                } else {
                  this.loading = false;
                  this.$message({
                    type: "error",
                    message: response.SYS_HEAD.ReturnMessage
                  });
                }
              })
              .catch(error => {
                this.loading = false;
                console.log(error);
              });

      }
    }
  };

</script>
<style lang="sass" scoped>

</style>
